package com.example.springmvcdemo.controller;

import com.example.springmvcdemo.model.UserInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {
    @RequestMapping(value = "/hello_world", method = RequestMethod.GET)
    public String hello() {
        return "hello";
    }

    @RequestMapping("/r1")
    public String r1(String name) {
        return "接收到的参数: " + name;
    }

    @RequestMapping("/r2")
    public String r2(UserInfo userInfo) {
        return userInfo.toString();
    }

    @RequestMapping("/r3")
    public String r3(@RequestParam("name") String username) {
        return "接收到的参数: " + username;
    }

    @RequestMapping("/r4")
    public String r4(String[] arr) {
        return Arrays.toString(arr);
    }

    @RequestMapping("/r5")
    public String r5(@RequestParam(value = "list", required = false) List<String> list) {
        return list.toString();
    }

    @RequestMapping("/r6")
    public String r6(@RequestBody UserInfo userInfo) {
        return userInfo.toString();
    }

    @RequestMapping("/r8/{name}/{age}")
    public String r8(@PathVariable Integer age, @PathVariable String name) {
        return "name: " + name + " age: " + age;
    }

    @RequestMapping("/r9")
    public String r9(@RequestPart MultipartFile file) throws IOException {
        String name = file.getOriginalFilename();
        file.transferTo(new File("D:/tmp/" + name));
        return "获取上传文件: " + file.getOriginalFilename();
    }
}
